package com.tieba.tieba.web;


import com.tieba.tieba.entity.User;
import com.tieba.tieba.service.IUserService;
import com.tieba.tieba.utils.ConstValue;
import com.tieba.tieba.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-04-08
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;


    /**
     * @return 登录页面
     */
    @GetMapping ("/login")
    public String login(){
        return "/user/login";
    }


    /**
     * @return 登录校验
     */
    @PostMapping ("/login")
    public String dologin(User user,String vercode,Model model, HttpSession session){

        //比较验证码
//        String code = (String) session.getAttribute("code");
//        if (!code.equals(vercode)) {
//            model.addAttribute("message", ConstValue.LOGIN_VERCODE_FAIL);
//            return "/others/error";
//        }
        //邮箱是否正确
        if (!userService.hasEmail(user.getUserEmail())) {
            model.addAttribute("message", ConstValue.LOGIN_NO_EMAIL);
            return "/others/error";
        }
        //密码是否正确
        if (!userService.checekPassword(user)) {
            model.addAttribute("message", ConstValue.LOGIN_PASSWORD_FAIL);
            return "/others/error";
        }
        //用户信息放入session
        session.setAttribute("user", user);
        //跳转到主页

        return "/index";
    }


    /**
     * @return 注册页面
     */
    @GetMapping ("/reg")
    public String reg(){
        return "/user/reg";
    }



    /**
     * @param user
     * @param repass
     * @param vercode
     * @param model
     * @param session
     * @return 注册校验
     */
    @PostMapping("/reg")
    public String doreg(User user, String repass, String vercode, Model model, HttpSession session){
        //比较验证码
        String code = (String) session.getAttribute("code");
        if (!code.equals(vercode)) {
            model.addAttribute("message", ConstValue.REG_VERCODE_FAIL);
            return "/others/error";
        }
        //检查数据库 邮箱是否存在 用户名是否存在
        if (userService.hasAccountName(user.getUserAccount())) {
            model.addAttribute("message", ConstValue.REG_SAME_ACCOUNT);
            return "/others/error";

        }
        if (userService.hasEmail(user.getUserEmail())) {
            model.addAttribute("message", ConstValue.REG_SAME_EMAIL);
            return "/others/error";
        }
        //存在报错
        //不存在 插入数据
        if (!userService.saveUser(user)) {
            model.addAttribute("message", ConstValue.REG_UNKNOWN);
            return "/others/error";
        }
        //用户信息放入session
        session.setAttribute("user", user);
        //跳转到主页

        return "/index";
    }

    /**
     * ajax获取验证码
     */
    @ResponseBody
    @RequestMapping(value = "/signcode", produces = {"application/json;charset=UTF-8"})
    public void getSecurityCode(HttpServletResponse response, HttpServletRequest request){
        // 通知浏览器不要缓存
        response.setHeader("Expires", "-1");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "-1");
        SecurityUtil util = SecurityUtil.Instance();
        // 将验证码输入到session中，用来验证
        String code = util.getString();
        request.getSession().setAttribute("code", code);
        // 输出打web页面
        try {
            ImageIO.write(util.getImage(), "jpg", response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
